package com.neu.architecture.service.impl;


import com.github.pagehelper.PageHelper;
import com.neu.architecture.dto.InfoParam;
import com.neu.architecture.dto.InfoQueryParam;
import com.neu.architecture.dto.ResourceParam;
import com.neu.architecture.mapper.ResourceMapper;
import com.neu.architecture.model.Info;
import com.neu.architecture.model.InfoExample;
import com.neu.architecture.model.Resource;
import com.neu.architecture.model.ResourceExample;
import com.neu.architecture.service.ArchitectureService;
import com.neu.architecture.service.ResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;  // 这个报错没问题的

    @Override
    public List<Resource> fetchList(Integer pageSize, Integer pageNum) {
        // pageHelper 设置
        PageHelper.startPage(pageNum, pageSize);
        // 参数为空 example, 相当于查询所有数据
        return resourceMapper.selectByExample(new ResourceExample());
    }

//    @Override
//    public List<Resource> searchResources(Integer pageSize, Integer pageNum, ResourceQueryParam param) {
//        PageHelper.startPage(pageNum, pageSize);
//        ResourceExample ResourceExample = new ResourceExample();
//        ResourceExample.Criteria criteria = ResourceExample.createCriteria();
//        if (param.getName() != null) {
//            criteria.andNameEqualTo(param.getName());
//        }
//        return resourceMapper.selectByExample(ResourceExample);
//    }

    @Override
    public int deleteResource(long id) {
        return resourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Resource> getResourceById(long id) {
        return List.of(resourceMapper.selectByPrimaryKey(id));
    }

    @Override
    public List<Resource> getResourceByInfoId(long infoId) {
        ResourceExample example = new ResourceExample();
        ResourceExample.Criteria criteria = example.createCriteria();
        criteria.andInfoIdEqualTo(infoId);
        return resourceMapper.selectByExample(example);
    }

    @Override
    public int updateResource(ResourceParam param, long id) {
        Resource Resource = new Resource();
        BeanUtils.copyProperties(param, Resource);
        Resource.setId(id);
        return resourceMapper.updateByPrimaryKeySelective(Resource);
    }

    @Override
    public int addResource(Resource Resource) {
        return resourceMapper.insert(Resource);
    }
}
